package board;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class MenuAjaxAction extends DispatchAction {
/*
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		return null;
	}
 */

	public ActionForward welcome(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {


		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<data>");
		NoticeBoardDAO nbDAO = new NoticeBoardDAO();		
		Vector<NoticeBoard> nlist = nbDAO.getBoardListPage(6, 1, 5);
		sb.append(this.setToXMLfromNoticeBoard(nlist).toString());

		FreeBoardDAO fbDAO = new FreeBoardDAO();
		FreeBoard fb = new FreeBoard();
		fb.setCono(5);
		fb.setFbname("자유게시판");
		Vector<FreeBoard> flist = fbDAO.getTopFiveListPage(fb, 1, 5);		
		sb.append(this.setToXMLfromFreeBoard(flist).toString());
		
		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		PhotoBoard pb = new PhotoBoard();
		pb.setCono(4);
		pb.setPbname("포토갤러리");
		Vector<PhotoBoard> plist = pbDAO.getTopNewList(4, "포토갤러리", 4);
		sb.append(this.setToXMLfromPhotoBoard(plist));
		
		sb.append("</data>");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		out.print(sb);
		return null;
	}
	
	public ActionForward freemain(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {


		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<data>");
		
		FreeBoardDAO fbDAO = new FreeBoardDAO();
		//cono=2,fbname=일본에서 살아남기 최신글 불러오기
		FreeBoard fb1 = new FreeBoard();
		fb1.setCono(2);
		fb1.setFbname("일본에서 살아남기");
		Vector<FreeBoard> flist1 = fbDAO.getTopFiveListPage(fb1, 1, 5);		
		sb.append(this.setToXMLfromFreeBoard(flist1).toString());
		//
		//cono=2,fbname=일본생활 필수정보 최신글 불러오기
		FreeBoard fb2 = new FreeBoard();
		fb2.setCono(2);
		fb2.setFbname("일본생활 필수정보");
		Vector<FreeBoard> flist2 = fbDAO.getTopFiveListPage(fb2, 1, 5);		
		sb.append(this.setToXMLfromFreeBoard(flist2).toString());
		//
		//cono=2,fbname=에피소드 및 경험담 최신글 불러오기
		FreeBoard fb3 = new FreeBoard();
		fb3.setCono(2);
		fb3.setFbname("에피소드 및 경험담");
		Vector<FreeBoard> flist3 = fbDAO.getTopFiveListPage(fb3, 1, 5);		
		sb.append(this.setToXMLfromFreeBoard(flist3).toString());
		//
		//cono=2,fbname=아나바다 게시판 최신글 불러오기
		FreeBoard fb4 = new FreeBoard();
		fb4.setCono(2);
		fb4.setFbname("아나바다 게시판");
		Vector<FreeBoard> flist4 = fbDAO.getTopFiveListPage(fb4, 1, 5);		
		sb.append(this.setToXMLfromFreeBoard(flist4).toString());
		//
		
		
		sb.append("</data>");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		out.print(sb);
		return null;
	}
	
	/**
	 * 보드메인
	 * */
	public ActionForward boardmain(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {


		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<data>");
		
		NoticeBoardDAO nbDAO = new NoticeBoardDAO();
		//cono=6, 전체공지사항 최신글 불러오기
		Vector<NoticeBoard> nlist1 = nbDAO.getBoardListPage(6, 1, 5);		
		sb.append(this.setToXMLfromNoticeBoard(nlist1).toString());
		//
		//cono=8, MYITEA뉴스 최신글 불러오기
		Vector<NoticeBoard> nlist2 = nbDAO.getBoardListPage(8, 1, 5);		
		sb.append(this.setToXMLfromNoticeBoard(nlist2).toString());//
		//cono=5, 전체 자유게시판 최신글 불러오기
		FreeBoardDAO fbDAO = new FreeBoardDAO();
		FreeBoard fb3 = new FreeBoard();
		fb3.setCono(5);
		fb3.setFbname("자유게시판");
		Vector<FreeBoard> flist3 = fbDAO.getTopFiveListPage(fb3, 1, 5);		
		sb.append(this.setToXMLfromFreeBoard(flist3).toString());
		//
		//QnA최신글 불러오기
		QnaBoardDAO qbDAO = new QnaBoardDAO();
		Vector<QnaBoard> qlist = qbDAO.getQnaBoardListPage(1, 5);		
		sb.append(this.setToXMLfromQnaBoard(qlist));
		//
		
		sb.append("</data>");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		out.print(sb);
		return null;
	}
	
	public ActionForward noticemain(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println("Request received...");
		
		return null;
	}
	
	public ActionForward datamain(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<data>");
		
		DataBoardDAO dao = new DataBoardDAO();
		
		//일본어자료실 불러오기
		DataBoard db1 = new DataBoard();
		db1.setCono(7);
		db1.setDbname("일본어자료실");
		Vector<DataBoard> list1 = dao.getTopList(db1);
//		System.out.println("test!!!!!"+list1.size());
		StringBuffer b1 = setToXMLfromDataBoard(list1);
		sb.append(b1);			
		
		//일본어강좌 불러오기
		DataBoard db2 = new DataBoard();
		db2.setCono(7);
		db2.setDbname("일본어강좌");
		Vector<DataBoard> list2 = dao.getTopList(db2);
		StringBuffer b2 = setToXMLfromDataBoard(list2);
		sb.append(b2);
		
		//프로그램자료실 불러오기
		DataBoard db3 = new DataBoard();
		db3.setCono(7);
		db3.setDbname("프로그램자료실");
		Vector<DataBoard> list3 = dao.getTopList(db3);
		StringBuffer b3 = setToXMLfromDataBoard(list3);
		sb.append(b3);
		
		//프로그램강좌 불러오기
		DataBoard db4 = new DataBoard();
		db4.setCono(7);
		db4.setDbname("프로그램강좌");
		Vector<DataBoard> list4 = dao.getTopList(db4);
		StringBuffer b4 = setToXMLfromDataBoard(list4);
		sb.append(b4);
		
		sb.append("</data>");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();		
		out.print(sb);
		
		return null;
	}
	
	private StringBuffer setToXMLfromNoticeBoard(Vector<NoticeBoard> list){
		StringBuffer sb = new StringBuffer();
		sb.append("<notices>");
		
		for(int i=0;i<list.size();i++){
			sb.append("<notice>");
			sb.append("<nbno>"+list.get(i).getNbno()+"</nbno>");
			sb.append("<cono>"+list.get(i).getCono()+"</cono>");
			sb.append("<mid>"+list.get(i)+"</mid>");
			String title = list.get(i).getNbtitle().length()>18?list.get(i).getNbtitle().substring(0,16)+"…":list.get(i).getNbtitle();
			sb.append("<nbtitle>"+title+"</nbtitle>");
			sb.append("<nbdate>"+list.get(i).getNbdate().substring(0,10)+"</nbdate>");
			sb.append("</notice>");
			
		}
		sb.append("</notices>");
		
		return sb;
	}
	
	private StringBuffer setToXMLfromFreeBoard(Vector<FreeBoard> list){
		StringBuffer sb = new StringBuffer();
		sb.append("<freeboard>");

		for(int i=0;i<list.size();i++){
			sb.append("<free>");
			sb.append("<fbno>"+list.get(i).getFbno()+"</fbno>");
			sb.append("<fbname>"+list.get(i).getFbname()+"</fbname>");
			sb.append("<cono>"+list.get(i).getCono()+"</cono>");
			sb.append("<mid>"+list.get(i)+"</mid>");
			String title = list.get(i).getFbtitle();
			sb.append("<fbtitle>"+(title.length()>18?title.substring(0,16)+"…":title)+"</fbtitle>");
			sb.append("<fbdate>"+list.get(i).getFbdate().substring(0,10)+"</fbdate>");			
			sb.append("</free>");
		}
		sb.append("</freeboard>");
		return sb;
	}
	
	private StringBuffer setToXMLfromDataBoard(Vector<DataBoard> list){
		StringBuffer sb = new StringBuffer();
		sb.append("<databoard>");
		
		for(int i=0;i<list.size();i++){
			sb.append("<db>");
			sb.append("<dbno>"+list.get(i).getDbno()+"</dbno>");
			sb.append("<cono>"+list.get(i).getCono()+"</cono>");
			sb.append("<dbname>"+list.get(i).getDbname()+"</dbname>");
			sb.append("<mid>"+list.get(i).getMid()+"</mid>");			
			String title = list.get(i).getDbtitle();
			sb.append("<dbtitle>"+(title.length()>18?title.substring(0,16)+"…":title)+"</dbtitle>");
			sb.append("<dbdate>"+list.get(i).getDbdate().substring(0,10)+"</dbdate>");
			sb.append("</db>");			
		}
		sb.append("</databoard>");
		
		return sb;
	}
	
	private StringBuffer setToXMLfromPhotoBoard(Vector<PhotoBoard> list){
		StringBuffer sb = new StringBuffer();
		sb.append("<photoboard>");
		
		for(int i=0;i<list.size();i++){
			sb.append("<photo>");
			sb.append("<pbno>"+list.get(i).getPbno()+"</pbno>");
			sb.append("<cono>"+list.get(i).getCono()+"</cono>");
			sb.append("<mid>"+list.get(i).getMid()+"</mid>");
			sb.append("<pbtitle>"+list.get(i).getPbtitle()+"</pbtitle>");
			sb.append("<pbsfn>"+list.get(i).getPbsfn()+"</pbsfn>");
			sb.append("<pbname>"+list.get(i).getPbname()+"</pbname>");
			sb.append("</photo>");
		}
		sb.append("</photoboard>");
		return sb;
	}

	private StringBuffer setToXMLfromQnaBoard(Vector<QnaBoard> list){
		StringBuffer sb = new StringBuffer();
		sb.append("<qnaboard>");
		for(int i=0;i<list.size();i++){
			sb.append("<qna>");
			sb.append("<qbno>"+list.get(i).getQbno()+"</qbno>");
			sb.append("<mid>"+list.get(i).getMid()+"</mid>");
			String title = list.get(i).getQbtitle();
			sb.append("<qbtitle>"+(title.length()>18?title.substring(0,16)+"…":title)+"</qbtitle>");
			sb.append("<qbdate>"+list.get(i).getQbdate().substring(0,10)+"</qbdate>");
			sb.append("</qna>");
		}
		sb.append("</qnaboard>");		
		return sb;
	}
	
}
